package cn.com.lyj6851.modules.wechat;

import cn.com.lyj6851.constant.ShopConstants;
import cn.com.lyj6851.exception.BadRequestException;
import cn.com.lyj6851.modules.mp.config.WxMpConfiguration;
import cn.com.lyj6851.modules.mp.domain.YdyWechatMenu;
import cn.com.lyj6851.modules.mp.service.YdyWechatMenuService;
import cn.com.lyj6851.utils.OrderUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(tags = "商城:微信菜單")
@RestController
@RequestMapping("api")
@SuppressWarnings("all")
@AllArgsConstructor
public class WechatMenuController {
    private final YdyWechatMenuService ydyWechatMenuService;

    @ApiOperation(value = "查询菜单")
    @GetMapping(value = "/ydyWechatMenu")
    @PreAuthorize("hasAnyRole('admin','WechatMenu_ALL','WechatMenu_SELECT')")
    public ResponseEntity getYdyWechatMenus(){
        return new ResponseEntity(ydyWechatMenuService.getOne(Wrappers.<YdyWechatMenu>lambdaQuery()
        .eq(YdyWechatMenu::getKey, ShopConstants.WECHAT_MENUS)), HttpStatus.OK);
    }

    @ApiOperation(value = "创建菜单")
    @PostMapping(value = "/ydyWechatMenu")
    @PreAuthorize("hasAnyRole('admin','WechatMenu_ALL','WechatMenu_CREATE')")
    public ResponseEntity create( @RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String jsonButton = jsonObject.get("buttons").toString();
        YdyWechatMenu ydyWechatMenu = new YdyWechatMenu();
        // WECHAT_MENUS = "wechat_menus"
        Boolean isExist = ydyWechatMenuService.isExist(ShopConstants.WECHAT_MENUS);
        // 把json转换成 WxMenu对象
        WxMenu menu = JSONObject.parseObject(jsonStr,WxMenu.class);
        WxMpService wxService = WxMpConfiguration.getWxMpService();
        if(isExist){
            ydyWechatMenu.setKey(ShopConstants.WECHAT_MENUS);
            ydyWechatMenu.setResult(jsonButton);
            // 更新
            ydyWechatMenuService.saveOrUpdate(ydyWechatMenu);
        }else {
            ydyWechatMenu.setKey(ShopConstants.WECHAT_MENUS);
            ydyWechatMenu.setResult(jsonButton);
            //获取精确到秒的时间戳
            ydyWechatMenu.setAddTime(OrderUtil.getSecondTimestampTwo());
            ydyWechatMenuService.save(ydyWechatMenu);
        }

        //创建菜单
        try {
            wxService.getMenuService().menuDelete();
            wxService.getMenuService().menuCreate(menu);
        }catch (WxErrorException e){
            throw new BadRequestException(e.getMessage());
        }

        return new ResponseEntity(HttpStatus.OK);
    }
}
